package com.lmk.mkcc.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.lmk.mkcc.constants.CommonConstants;
import com.lmk.mkcc.domain.BlackPhone;
import com.lmk.mkcc.mapper.BlackPhoneMapper;
import com.lmk.mkcc.service.IBlackPhoneService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author kun
 * @since 2022-10-01
 */
@Service
public class BlackPhoneServiceImpl extends ServiceImpl<BlackPhoneMapper, BlackPhone> implements IBlackPhoneService {
    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;
    @Override
    public boolean insert(BlackPhone entity) {
        entity.setCreateTime(new Date());
        entity.setUsername(entity.getPhone());
        RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
        HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
        entity.setIp(request.getRemoteAddr());
        // 将数据保存到redis中
        EntityWrapper<BlackPhone> wrapper = new EntityWrapper<>();
        List<BlackPhone> blackPhones = selectList(wrapper);
        redisTemplate.opsForValue().set(CommonConstants.BLACK_PHONE_KEY,blackPhones);
        return super.insert(entity);


    }

    /*@Override
    public List<BlackPhone> selectList(Wrapper<BlackPhone> wrapper) {
        List<BlackPhone> blackPhones = super.selectList(wrapper);
        redisTemplate.opsForValue().set(CommonConstants.BLACK_PHONE_KEY,blackPhones);
        return super.selectList(wrapper);
    }

    @Override
    public Page<BlackPhone> selectPage(Page<BlackPhone> page) {
        Page<BlackPhone> blackPhonePage = super.selectPage(page);
        return super.selectPage(page);
    }*/
}
